package com.thb.pay.agent.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.thb.pay.db.entity.SuperEntity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * <p>
 * 代理商-账户表
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
@Accessors(chain = true)
@TableName("agent_account")
public class AgentAccount extends SuperEntity<AgentAccount> {

    public final static byte AGENT_BIZ_TYPE_PROFIT = 1;         // 代理商分润
    public final static byte AGENT_BIZ_TYPE_REMIT = 2;          // 代理商打款
    public final static byte AGENT_BIZ_TYPE_CHANGE_BALANCE = 3;          // 代理商调账
    private static final long serialVersionUID = 1L;
    /**
     * 代理商ID
     */
    private Long agentId;

    /**
     * 账户余额
     */
    private Long balance;

    /**
     * 不可用余额
     */
    private Long unBalance;

    /**
     * 保证金
     */
    private Long securityMoney;

    /**
     * 总收益
     */
    private Long totalIncome;

    /**
     * 总支出
     */
    private Long totalExpend;

    /**
     * 今日收益
     */
    private Long todayIncome;

    /**
     * 今日支出
     */
    private Long todayExpend;

    /**
     * 可结算金额
     */
    private Long settAmount;

    /**
     * 账户状态,1-可用,0-停止使用
     */
    private Integer status;

    /**
     * 获取可用余额
     *
     * @return
     */
    public Long getAvailableBalance() {
        return this.balance - this.unBalance;
    }

    /**
     * 获取实际可结算金额
     *
     * @return
     */
    public Long getAvailableSettAmount() {
        Long subSettAmount = this.settAmount - this.unBalance;
        if (getAvailableBalance().compareTo(subSettAmount) == -1) {
            return getAvailableBalance();
        }
        return subSettAmount;
    }

    /**
     * 验证可用余额是否足够
     *
     * @param amount
     * @return
     */
    public boolean availableBalanceIsEnough(Long amount) {
        return this.getAvailableBalance().longValue() >= amount;
    }
}
